<?php

namespace zhtfrontend\modules\cardmanage\controllers;

use common\components\helpers\Fun;
use common\models\datas\Cards;
use common\models\datas\CardsBatch;
use yii\helpers\ArrayHelper;
use zhtfrontend\modules\cardmanage\components\rest\ActiveController;
use yii\web\Response;

class ApplyController extends ActiveController
{
    public $modelClass = 'common\models\datas\CardsBatch';

    public $serializer = [
        'class' => 'zhtfrontend\modules\cardmanage\components\rest\Serializer',
        'collectionEnvelope' => 'items',
    ];

    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;
        return $behaviors;
    }
    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index'], $actions['create'], $actions['update'], $actions['delete'], $actions['view']);
        return $actions;
    }

    public function actionList(){
        $modelClass = $this->modelClass;
        $data = \Yii::$app->request->get();
        $user_id = ArrayHelper::keyExists('user_id', $data) ? $data['user_id'] : '';
        $site=ArrayHelper::keyExists('site', $data) ? $data['site'] : 2;
        if($site=='qdlcaks'){
            $site=2;
        }
        $card_apply_list=$modelClass::find()->where(['applicant_id' => $user_id, 'site_id' => $site,'is_delete'=>0])->all();
        $card_apply_listn=[];
        foreach ($card_apply_list as $k => $v) {
            switch ($v->status){
                case 0:
                    $v->status='审核中';break;
                case 1:
                    $v->status='通过';break;
                case 2:
                    $v->status='失败';break;
            };
            $card_apply_listn[$k] = [
                'apply_id'=>$v->id,
                'applicant'=>isset($v->cardsIssuerinfor->name)?$v->cardsIssuerinfor->name:'',
                'apply_time'=>date('Y-m-d', $v->created_at),
                'title'=>$v->title,
                'status'=>$v->status,
            ];
        }
        return ['items'=>$card_apply_listn];
    }
    public function actionDetail(){
        $modelClass = $this->modelClass;
        $data = \Yii::$app->request->get();
        $apply_id = ArrayHelper::keyExists('apply_id', $data) ? $data['apply_id'] : '';
        $site=ArrayHelper::keyExists('site', $data) ? $data['site'] : 2;
        if($site=='qdlcaks'){
            $site=2;
        }
        $card_apply_detail=$modelClass::find()->where(['id' => $apply_id, 'site_id' => $site,'is_delete'=>0])->one();
        switch ($card_apply_detail->status){
            case 0:
                $card_apply_detail->status='审核中';break;
            case 1:
                $card_apply_detail->status='通过';break;
            case 2:
                $card_apply_detail->status='失败';break;
        };
        switch ($card_apply_detail->card_type){
            case 0:
                $card_apply_detail->card_type='实体卡';break;
            case 1:
                $card_apply_detail->card_type='虚拟红包';break;
        };
        $cardno_begin=Cards::find()->where(['id'=>$card_apply_detail->cardno_begin])->one();
        $cardno_end=Cards::find()->where(['id'=>$card_apply_detail->cardno_end])->one();
        $card_apply_detail->cardno_begin=isset($cardno_begin->card_no)?$cardno_begin->card_no:'';
        $card_apply_detail->cardno_end=isset($cardno_end->card_no)?$cardno_end->card_no:'';
        $card_apply_detail->created_at=date('Y-m-d', $card_apply_detail->created_at);
        $card_apply_detail->grant_time=date('Y-m-d', $card_apply_detail->grant_time);
        return ['items'=>$card_apply_detail];
    }

    public function actionApply(){
        $modelClass = $this->modelClass;
        $data = \Yii::$app->request->get();
        $user_id = ArrayHelper::keyExists('user_id', $data) ? $data['user_id'] : '';
        $apply_title=ArrayHelper::keyExists('apply_title', $data) ? $data['apply_title'] : '';
        $apply_cardtype=ArrayHelper::keyExists('apply_cardtype', $data) ? $data['apply_cardtype'] : '';
        $apply_descrip=ArrayHelper::keyExists('apply_descrip', $data) ? $data['apply_descrip'] : '';
        $apply_number=ArrayHelper::keyExists('apply_number', $data) ? $data['apply_number'] : '';
        $apply_integral=ArrayHelper::keyExists('apply_integral', $data) ? $data['apply_integral'] : '';
        $apply_cash=ArrayHelper::keyExists('apply_cash', $data) ? $data['apply_cash'] : '';
        $model = new $modelClass;
        $model->site_id=2;
        $model->applicant_id=(int)$user_id;
        $model->apply_number=(int)$apply_number;
        $model->apply_reason = $apply_descrip;
        $model->card_type = (int)$apply_cardtype;
        $model->title = $apply_title;
        $model->integral = (int)$apply_integral;
        $model->cash = (int)$apply_cash;
        if ($model->save(false)){
            return ['items'=>$model];
        }else{
            return ['statusCode' => 0, 'message' => 'error', 'result' => '提交申请失败'];
        }
    }

}